/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package percistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import model.Aluno;
import model.Instrutor;
import model.TipoExercicio;

/**
 *
 * @author Webert
 */
public class InstrutorDAO {
      private static InstrutorDAO instance= new InstrutorDAO();
  
  private InstrutorDAO() {}
  
  public static InstrutorDAO getInstance() {
    return instance;
  }
  
      public void salvar(Instrutor instrutor) throws ClassNotFoundException{
    Connection conn = null;
    Statement st  = null;

    try {
      conn = DatabaseLocator.getInstance().getConnection();
      st = conn.createStatement();
      st.execute("insert into instrutor (nome, email, dataNascimento, endereco, telefone, sexo, dataCadastro, formacao)" +
        " values ('" + instrutor.getNome() + "', '" + instrutor.getEmail() + "','" + instrutor.getDataNascimento() + "','" + instrutor.getEndereco() + "','" + instrutor.getTelefone() + "','" + instrutor.getSexo() + "', '" + instrutor.getDataCadastro() + "', '" + instrutor.getFormacao() + "')");
    } catch(SQLException e) {
        e.printStackTrace();
    } finally {
      closeResources(conn, st);
    }
   }
   
   public void alterar(Aluno aluno){
   
   }
   
   public void excluir(Integer id){
   
   }
   
   public void consultar(Integer id){
   
   }
  
    public void closeResources(Connection conn, Statement st) {
    try {
      if(st!=null) st.close();
      if(conn!=null) conn.close();
      
    } catch(SQLException e) {
      
    }
  }
     public List<Instrutor> carregarInstrutores() throws ClassNotFoundException, SQLException{
        Connection conn = DatabaseLocator.getInstance().getConnection();
        PreparedStatement stmt = conn.prepareStatement("SELECT * FROM instrutor ORDER BY idInstrutor");
        ResultSet rs = stmt.executeQuery();
        List<Instrutor> listInstrutores = new ArrayList<Instrutor>();
        while (rs.next()) {
            // criando o objeto
            Instrutor instrutor = new Instrutor();
            instrutor.setNome(rs.getString("nome"));
            instrutor.setFormacao(rs.getString("formacao"));
            // adicionando o objeto à lista
            listInstrutores.add(instrutor);
        }
        rs.close();
        stmt.close();
        return listInstrutores;
   }  
}
